package com.dotflat.check.service.impl;

import com.dotflat.check.domain.RelId;
import com.dotflat.check.repository.RelIdRepository;
import com.dotflat.check.service.RelIdService;
import com.dotflat.util.excelUtil.StringUtils;
import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class RelIdServiceImpl  implements RelIdService {
    @Autowired
    private RelIdRepository relIdRepository;

    @Override
    public List<RelId> findByUserIdIn(List<String> userIds) {
        List<RelId> list = relIdRepository.findByUserIdIn(userIds);
        for (int i = 0; i < userIds.size(); i++) {
            if (StringUtils.equals(userIds.get(i),"") || null == userIds.get(i)){
                continue;
            }
            boolean v = false;
            for (int j = 0; j < list.size(); j++) {
                if (StringUtils.equals(userIds.get(i),list.get(j).getUserId())){
                    v=true;
                    continue;
                }
            }
            if (!v){
                RelId relId = new RelId();
                relId.setUserId(userIds.get(i));
                relIdRepository.save(relId);
                list.add(relId);
            }
        }
        return list;
    }

    @Override
    public List<RelId> findByIdIdIn(List<Integer> ids) {
        List<RelId> list = relIdRepository.findByIdIn(ids);
        return list;
    }

    @Override
    public RelId findById(Integer id) {
        RelId relId = relIdRepository.findById(id);
        if (null == relId){
            throw new BusinessException(ErrorCode.UNDEFINED, "没有该园区ID");
        }
        return relId;
    }
}